package com.hxc.springboot.judge.codesandbox;

import com.hxc.springboot.judge.codesandbox.model.ExecuteCodeRequest;
import com.hxc.springboot.judge.codesandbox.model.ExecuteCodeResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * 代码沙箱代理类
 * 提供日志记录、监控等增强功能
 */
@Slf4j
public class CodeSandboxProxy implements CodeSandbox {

    private final CodeSandbox codeSandBox;

    public CodeSandboxProxy(CodeSandbox codeSandBox) {
        this.codeSandBox = codeSandBox;
    }

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest request) {
        log.info("代码沙箱执行请求：{}", request);

        long startTime = System.currentTimeMillis();
        try {
            ExecuteCodeResponse response = codeSandBox.executeCode(request);
            long endTime = System.currentTimeMillis();
            log.info("代码沙箱执行成功，耗时: {}ms，响应：{}", endTime - startTime, response);
            return response;
        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("代码沙箱执行失败，耗时: {}ms，错误信息：{}", endTime - startTime, e.getMessage());
            throw e;
        }
    }
}
